<?php

namespace app\admin\service;

use app\model\ConsultOfflineResultModel;
use app\model\ConsultOnlineResultModel;
use app\model\group_activity\GroupActivityTaskUserResultModel;
use app\model\ProxyEvaluationResultModel;
use app\model\RatingScaleQuestionModel;
use app\model\school\SchoolCourseChapterResultModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultContinuityModel;
use app\model\TaskUserResultModel;
use app\model\training\TrainingTaskUserResultModel;
use app\model\WorkersTaskUserResultContinuityModel;
use app\model\WorkersTaskUserResultModel;
use think\Service;

class ReportService extends Service
{
    /**
     * 自我测评详情
     * @param int $id 自我测评列表id
     * @return array
     */
    public function getSelfEvaluationReportInfo(int $id): array
    {
        $info = $this->getSelfReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function getSelfReportInfo(int $id)
    {
        $SelfEvaluationResultModel = new SelfEvaluationResultModel();
        $data = $SelfEvaluationResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'SelfFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['SelfFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['SelfFactorScore'][0]['high_range']){
                $data['SelfFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 他人代测详情
     * @param int $id 他人代测列表id
     * @return array
     */
    public function getProxyTestingReportInfo(int $id): array
    {
        $info = $this->getProxyReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function getProxyReportInfo(int $id)
    {
        $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
        $data = $ProxyEvaluationResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ProxyFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ProxyFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ProxyFactorScore'][0]['high_range']){
                $data['ProxyFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 任务测评单人报告
     * @param int $id 答题结果id
     */
    public function getTaskReportInfo(int $id)
    {
        $TaskUserResultModel = new TaskUserResultModel();
        $data = $TaskUserResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'TaskFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['TaskFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['TaskFactorScore'][0]['high_range']){
                $data['TaskFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 无线测评任务测评单人报告
     * @param int $id 答题结果id
     */
    public function getWirelessTaskReportInfo(int $id)
    {
        $WorkersTaskUserResultModel = new WorkersTaskUserResultModel();
        $data = $WorkersTaskUserResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'TaskFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['TaskFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['TaskFactorScore'][0]['high_range']){
                $data['TaskFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 线下预约任务测评结果报告
     * @param $id
     */
    public function getOfflineEvaluationReportInfo($id)
    {
        $info = $this->OfflineReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }
    public function OfflineReportInfo($id)
    {
        $ConsultOfflineResultModel = new ConsultOfflineResultModel();
        $data = $ConsultOfflineResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ConsultOfflineFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ConsultOfflineFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ConsultOfflineFactorScore'][0]['high_range']){
                $data['ConsultOfflineFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 线上咨询任务测评结果报告
     * @param $id
     */
    public function getOnlineEvaluationReportInfo($id)
    {
        $info = $this->OnlineReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function OnlineReportInfo($id)
    {
        $ConsultOnlineResultModel = new ConsultOnlineResultModel();
        $data = $ConsultOnlineResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ConsultOnlineFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ConsultOnlineFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ConsultOnlineFactorScore'][0]['high_range']){
                $data['ConsultOnlineFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }


    /**
     * 团体活动测评详情
     * @param int $id 团体测评结果id
     * @return array
     */
    public function getGroupActivityEvaluationReportInfo(int $id): array
    {
        $info = $this->getGroupActivityReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function getGroupActivityReportInfo(int $id)
    {
        $GroupActivityTaskUserResultModel = new GroupActivityTaskUserResultModel();
        $data = $GroupActivityTaskUserResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ResultFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ResultFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ResultFactorScore'][0]['high_range']){
                $data['ResultFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }



    /**
     * 心理学堂测评详情
     * @param int $id 测评结果id
     * @return array
     */
    public function getSchoolEvaluationReportInfo(int $id): array
    {
        $info = $this->getSchoolReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function getSchoolReportInfo(int $id)
    {
        $SchoolCourseChapterResultModel = new SchoolCourseChapterResultModel();
        $data = $SchoolCourseChapterResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ResultFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ResultFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ResultFactorScore'][0]['high_range']){
                $data['ResultFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }



    /**
     * 培训讲座测评详情
     * @param int $id 测评结果id
     * @return array
     */
    public function getTrainingEvaluationReportInfo(int $id): array
    {
        $info = $this->getTrainingReportInfo($id);
        //获取量表题目
        $AnswerIds = json_decode($info['question_answer'], true);
        $RatingScaleQuestionModel = new RatingScaleQuestionModel();
        $QuestionList = $RatingScaleQuestionModel->field('id,num,name,type')
            ->where('rating_scale_id', '=', $info['rating_scale_id'])
            ->with([
                'RatingScaleQuestionAnswer' => function ($query) {
                    $query->field('id,question_id,name,score');
                }
            ])->select()->toArray();
        foreach ($QuestionList as &$QuestionAnswer) {
            foreach ($QuestionAnswer['RatingScaleQuestionAnswer'] as &$item) {
                if (in_array($item['id'], $AnswerIds)) {
                    $item['select'] = 1;
                } else {
                    $item['select'] = 0;
                }
            }
        }
        return ['info' => $info, 'QuestionList' => $QuestionList];
    }

    public function getTrainingReportInfo(int $id)
    {
        $TrainingTaskUserResultModel = new TrainingTaskUserResultModel();
        $data = $TrainingTaskUserResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'ResultFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['ResultFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['ResultFactorScore'][0]['high_range']){
                $data['ResultFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }

    /**
     * 连评
     * @param int $id
     * @return array
     */
    public function getTaskContinuityReportInfo(int $id)
    {
        $TaskUserResultContinuityModel = new TaskUserResultContinuityModel();
        $data = $TaskUserResultContinuityModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['departmentName', 'unitName', 'roleName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'TaskFactorScoreContinuity' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['TaskFactorScoreContinuity'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['TaskFactorScoreContinuity'][0]['high_range']){
                $data['TaskFactorScoreContinuity'][0]['high_range'] = $v['high_range'];
            }
        }
        $UserService = new UserService($this->app);
        $data['department_name'] = $UserService->getNotDelDepartmentName($data['unit_id'],$data['department_id']);
        return $data;
    }


    /**
     * 教职工任务测评报告
     * @param int $id
     * @return array
     */
    public function getWorkersTaskReportInfo(int $id)
    {
        $WorkersTaskUserResultModel = new WorkersTaskUserResultModel();
        $data = $WorkersTaskUserResultModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['unitName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'TaskFactorScore' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['TaskFactorScore'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['TaskFactorScore'][0]['high_range']){
                $data['TaskFactorScore'][0]['high_range'] = $v['high_range'];
            }
        }
        return $data;
    }

    /**
     * 教职工任务测评报告-预警连续测评
     * @param int $id
     * @return array
     */
    public function getWorkersTaskContinuityReportInfo(int $id)
    {
        $WorkersTaskUserResultContinuityModel = new WorkersTaskUserResultContinuityModel();
        $data = $WorkersTaskUserResultContinuityModel->with([
            'ratingScale' => function ($query) {
                $query->with([
                    'ratingScaleTypeName',
                    'RatingScaleMode'
                ]);
            },
            'user' => function ($query) {
                $query->with(['unitName']);
            },
            'RatingScaleFactorWarningModel' => function ($query) {
                $query->with(['factorName']);
            },
            'TaskFactorScoreContinuity' => function ($query) {
                $query->with([
                    'RatingScaleFactor' => function ($query) {
                        $query->with(['RatingScaleFactorLevel']);
                    },
                    'RatingScaleFactorLevel'
                ]);
            }
        ])->find($id);
        if (!$data) return [];
        $data = $data->toArray();
        foreach ($data['TaskFactorScoreContinuity'][0]['rating_scale_factor_level']??[] as $v){
            if ($v['high_range'] >  $data['TaskFactorScoreContinuity'][0]['high_range']){
                $data['TaskFactorScoreContinuity'][0]['high_range'] = $v['high_range'];
            }
        }
        return $data;
    }
}